Type.registerNamespace('ModWatch');

ModWatch.ModManager = function() {
	ModWatch.ModManager.initializeBase(this);

	this._modWatchers = new Array();
	this._hasHiddenChange = false;
}

ModWatch.ModManager.prototype = {

	_onPageLoading : function() {
		// reset the hasHiddenChange flag... it will be reset if needed
		this._hasHiddenChange = false;
	},

	registerModWatcher : function(modWatcher) {
		if (!ModWatch.IModWatcher.isImplementedBy(modWatcher))
			throw new Error.argumentType('modWatcher', Object.getType(modWatcher), ModWatch.IModWatcher);

		Array.add(this._modWatchers, modWatcher);
	},

	removeModWatcher : function(modWatcher) {
		Array.remove(this._modWatchers, modWatcher);
	},
	
	isChangeDetected : function(modGroup)
	{
		if (this.get_hasHiddenChange())
			return true;

		var mwList = this.getModWatchers(modGroup);
		var hasChange = false;

		var checkChangeFunction = function(element, index, array)
		{
			if (element.hasChanged())
				hasChange = true;
		}

		Array.forEach(mwList, checkChangeFunction, null);
		
		return hasChange;
	},

	getModWatchers : function(modGroup)
	{
		if (!modGroup || modGroup == null)
			modGroup = '';

		var mwList = new Array();

		var findModWatchers = function(element, index, array)
		{
			if (element.get_modGroup() == this)
				Array.add(mwList, element);
		}
		
		Array.forEach(this._modWatchers, findModWatchers, modGroup);

		return mwList;
	},
	
	registerHiddenChange : function() {
		this._hasHiddenChange = true;
	},

	get_hasHiddenChange : function() {
		return this._hasHiddenChange;
	}

}

ModWatch.ModManager.registerClass('ModWatch.ModManager', Sys.Component);

if (!ModWatch.ModManagerInstance)
	ModWatch.ModManagerInstance = $create(ModWatch.ModManager, null, null, null, null);

ModWatch.ModManager.InitializeModManagerInstance = function()
{
	Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(ModWatch.ModManager.LoadingModManagerInstance)
}

ModWatch.ModManager.LoadingModManagerInstance = function()
{
	ModWatch.ModManagerInstance._onPageLoading();
}

if (typeof(Sys) !== 'undefined')
{
	Sys.Application.add_init(ModWatch.ModManager.InitializeModManagerInstance);
	Sys.Application.notifyScriptLoaded();
}
